/*
 * @Descripttion:
 * @Author: liang
 * @Date: 2020-03-23 10:08:16
 * @LastEditors: liang
 * @LastEditTime: 2020-08-07 09:23:50
 */
import Cookies from 'js-cookie'
export default {
  data () {
    return {

      visible: false,
      url: '',
      num: 0,
      fileList: []
    }
  },
  methods: {
    init () {
      this.visible = true
      this.url = `${window.SITE_CONFIG['apiURL'] + this.uploadUrl}?token=${Cookies.get('studyToken')}`
      this.num = 0
      this.fileList = []
    },
    // 上传之前
    beforeUploadHandle (file) {
      if (file.type !== 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' && file.type !== 'application/vnd.ms-excel') {
        if (file.type === '') {
          let extension = file.name.substring(file.name.lastIndexOf('.') + 1)
          if (extension !== 'xlsx' && extension !== 'xls') {
            this.$message.warning(this.$t('upload.tip', { 'format': 'xls、xlsx' }))
            return false
          }
        } else {
          this.$message.error(this.$t('upload.tip', { 'format': 'xls、xlsx' }))
          return false
        }
      }
      this.num++
    },
    // 上传成功
    successHandle (res, file, fileList) {
      this.num--
      if (res.data.success !== true) {
        this.$emit('refreshDataList')
        return this.$message.error(res.data.msg)
      }
      this.fileList = fileList

      if (this.num === 0) {
        this.$message({
          message: this.$t('prompt.success'),
          type: 'success',
          duration: 500,
          onClose: () => {
            this.visible = false
            this.$emit('refreshDataList')
          }
        })
      }
    },
    downloadExcel () {
      var url = `${window.SITE_CONFIG['apiURL'] + this.downloadUrl}?token=${Cookies.get('studyToken')}`
      window.location.href = url
    }
  }
}
